#!/usr/bin/python2.4
#
# Copyright 2011 Google Inc. All Rights Reserved.

from google.appengine.ext import db

class UserProfile(db.Model):
    email = db.StringProperty()
    password = db.StringProperty()
    last_name = db.StringProperty()
    first_name = db.StringProperty()
    nick_name = db.StringProperty()
    reg_date = db.DateTimeProperty(auto_now_add=True)
    account_type = db.IntegerProperty()

def UserKey(user_email=None):
    return db.Key.from_path('User', user_email or 'default@gmail.com')

def VerifyUser(email, password):
    user_profiles = db.GqlQuery("SELECT * FROM UserProfile WHERE ANCESTOR IS :1", UserKey(email))
    for user_profile in user_profiles:
        if user_profile.email == email and user_profile.password == password:
            return user_profile
        else:
            return None

def HasUser(email):
    user_profiles = db.GqlQuery("SELECT * FROM UserProfile WHERE ANCESTOR IS :1", UserKey(email))
    for user_profile in user_profiles:
        if user_profile.email == email:
            return True
        else:
            return False

def GetUserByEmail(user_email):
    user_profiles = db.GqlQuery("SELECT * FROM UserProfile WHERE ANCESTOR IS :1", UserKey(user_email))
    for user_profile in user_profiles:
        if user_profile.email == user_email:
            return user_profile
        else:
            return None

def GetAllUserList():
    user_profiles = db.GqlQuery("SELECT * FROM UserProfile")
    return user_profiles

def DeleteUser(user_email):
    user_profiles = db.GqlQuery("SELECT * FROM UserProfile WHERE ANCESTOR IS :1", UserKey(user_email))
    db.delete(user_profiles)